package com.blb.mapper;

import com.blb.entity.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Map;
import java.util.Set;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author HuaHao
 * @since 2019-11-15
 */
public interface UserMapper extends BaseMapper<User> {

    //通过用户名查询用户
    @Select("SELECT * FROM s_user WHERE BINARY user_name =#{userName}")
    User getUserByUsername(String userName);

    //添加用户
    @Insert("INSERT INTO s_user(id,user_name,pass_word,salt,user_image,real_name,sex,bir_year,bir_month,bir_day,user_email,user_tel,user_status,company_id,query_times) VALUES(#{user.userId},#{user.userName},#{user.passWord},#{user.salt},#{user.userImage},#{user.realName},#{user.sex},#{user.birYear},#{user.birMonth},#{user.birDay},#{user.userEmail},#{user.userTel},#{user.userStatus},#{user.companyId},#{user.queryTimes}")
    void addUser(User user);

    @Update("UPDATE s_user SET query_times = query_times + 1 WHERE id = #{id}")
    void update(String id);


    //根据用户名查询角色
    @Select("SELECT role_name FROM s_role r LEFT JOIN s_user_role ur ON r.id = ur.role_id LEFT JOIN s_user u ON u.id = ur.user_id WHERE u.user_name = #{userName}")
    Set<String> getRoleByUserName(String userName);


    //根据用户查询公司所有信息
    @Select("SELECT * FROM s_user u LEFT JOIN s_company c ON u.company_id = c.id WHERE u.id = #{userId}")
    Map<String,Object> getUserAndCompanyByUser(String userId);


    //根据用户名查询所有权限
    @Select("SELECT m.menu_permission FROM s_user u LEFT JOIN s_user_role ur ON u.id=ur.user_id LEFT JOIN s_role r ON ur.role_id=r.id LEFT JOIN s_role_menu rm ON r.id=rm.role_id LEFT JOIN s_menu m ON rm.menu_id=m.id WHERE u.user_name=#{userName} AND m.menu_permission IS NOT NULL")
    Set<String> getMenusByUserName(String userName);


    //根据用户名查询所有权限
    @Select("SELECT menu_permission FROM s_menu WHERE menu_permission IS NOT NULL")
    Set<String> getAllMenusToManage();
}
